data_train<-read.csv("train.csv")
head(data_train)
str(data_train)

data_test<-read.csv("test.csv")
head(data_test)
str(data_test)
library(ggplot2)
#install.packages("vcd")
library(vcd)
#1.讨论影响生存的因素
##a. 舱位等级对生存的影响
#pclass_s<-xtabs(~Survived+Pclass,data = data_train)
#pclass_s
pclass_s<-table(data_train$Survived,data_train$Pclass)
pclass_s
pclass_s_prop<-prop.table(pclass_s,2)*100
pclass_s_prop
#chisq.test(pclass_s)
#ggplot(data=data_train,aes(x=Pclass,trace(Survived))+
 # geom_bar()+
  #labs(title="舱位等级与生存的关系",x="pclass")
  ggplot(data = data_train, aes(x = Pclass, fill = factor(Survived)))+geom_bar(stat='count', position='dodge') + scale_x_continuous(breaks=c(1:3)) + labs(x = '舱位等级与生存的关系')
##b.性别对生存的影响（女士优先？）
  sex_s<-table(data_train$Survived,data_train$Sex)
  sex_s
  sex_s_prop<-prop.table(sex_s,2)*100
  sex_s_prop
  ggplot(data = data_train, aes(x = Sex, fill = factor(Survived)))+geom_bar(stat='count', position='dodge')  + labs(x = '性别与生存的关系')
##c.年龄对生存的影响（孩子优先）
  #agedaata<-data_train$Age
  agedata<-cut(data_train$Age, breaks = c(0, 15, 30, 45, 60, 75, 90), include.lowest=T,labels = c('0-15岁', '16-30岁', '31-45岁', '46-60岁', '61-75岁', '76-90岁' ))
  data_train$agedata <- agedata
  age_s<-table(data_train$Survived,data_train$agedata)
  age_s
  age_s_prop<-prop.table(age_s,2)*100
  age_s_prop
  survived<-data_train$Survived
  ggplot(data=data.frame(data_train$Survived,agedata), aes(x = agedata, fill = factor(survived)))+geom_bar(stat='count', position='dodge') +labs(x = '年龄与生存的关系')
##d.亲人数量对生存的影响
###旁系亲属Sibsp
  sibsp_s<-table(data_train$Survived,data_train$SibSp)
  sibsp_s
  sibsp_s_prop<-prop.table(sibsp_s,2)*100
  sibsp_s_prop
  ggplot(data = data_train, aes(x = SibSp, fill = factor(Survived)))+geom_bar(stat='count', position='dodge') + scale_x_continuous(breaks=c(0:8)) + labs(x = '旁系亲属数量与生存的关系')
###直系亲属Parch
  parch_s<-table(data_train$Survived,data_train$Parch)
  parch_s
  parch_s_prop<-prop.table(parch_s,2)*100
  parch_s_prop
  ggplot(data = data_train, aes(x = Parch, fill = factor(Survived)))+geom_bar(stat='count', position='dodge')  + scale_x_continuous(breaks=c(0:6))+ labs(x = '直系亲属数量与生存的关系')
##家庭成员数量对生存的影响（旁系亲属加直系亲属）
  families<-data_train$SibSp+data_train$Parch
  families
  families_s<-table(data_train$Survived,families)
  families_s
  families_s_prop<-prop.table(families_s,2)*100
  families_s_prop
  ggplot(data = data.frame(data_train$Survived,families), aes(x =families, fill = factor(Survived)))+geom_bar(stat='count', position='dodge')  + scale_x_continuous(breaks=c(0:10))+ labs(x = '亲属数量与生存的关系')
##e. 携带现金数量对生存几率的影响
  faredata<-cut(data_train$Fare, breaks = c(0,50,100,600), include.lowest=T,labels = c('贫穷', '中等', '富有'))
  data_train$faredata <- faredata
  fare_s<-table(data_train$Survived,data_train$faredata)
  fare_s
  fare_s_prop<-prop.table(fare_s,2)*100
  fare_s_prop
  survived<-data_train$Survived
  ggplot(data=data.frame(data_train$Survived,faredata), aes(x = faredata, fill = factor(survived)))+geom_bar(stat='count', position='dodge') +labs(x = '携带现金与生存的关系')
 #携带不同数量现金乘客的其他属性
  plot_FP <- ggplot(data=data.frame(data_train$Pclass,faredata), aes(x = faredata, fill = factor(data_train.Pclass )))+
      geom_bar(stat='count', position='dodge') +
      labs(x = 'Fare')+ scale_fill_brewer(palette = "Blues") + 
      theme_bw() +
      # geom_text(aes(x = faredata, y =100 ),label = 1, position = 200)
    geom_text(stat='count', aes(label=..count..), position=position_dodge(0.9), vjust=-0.1) 
  plot_FS <- ggplot(data = data.frame(data_train$Sex, faredata), aes(x =faredata , fill = factor(data_train.Sex)))+geom_bar(stat='count', position='dodge') + labs(x = 'Fare') +   scale_fill_brewer(palette = "Blues") + theme_bw()+
    geom_text(stat='count', aes(label=..count..), position=position_dodge(0.9), vjust=-0.1) 
  plot_FA <- ggplot(data = data.frame(agedata, faredata), aes(x =faredata , fill = factor(agedata)))+geom_bar(stat='count', position='dodge') + labs(x = 'Fare') + scale_fill_brewer(palette = "Blues") + theme_bw()+
    geom_text(stat='count', aes(label=..count..), position=position_dodge(0.9), vjust=-0.1) 
  plot_FF <- ggplot(data = data.frame(families, faredata), aes(x =faredata, fill = factor(families)))+geom_bar(stat='count', position='dodge') + labs(x = 'Fare') +scale_fill_brewer(palette = "Blues") + theme_bw()+
    geom_text(stat='count', aes(label=..count..), position=position_dodge(0.9), vjust=-0.1) 
  install.packages(grid)
  library(grid)
  grid.newpage()
  pushViewport(viewport(layout = grid.layout(2, 2)))
  vplayout = function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
  print(plot_FP, vp = vplayout(1, 1))
  print(plot_FS, vp = vplayout(1, 2))
  print(plot_FA, vp = vplayout(2, 1))
  print(plot_FF, vp = vplayout(2, 2))
 